import datetime
import geopy
import math

import common
import warningExtractor
import stormReportExtractor
import radarExtractor
from geopy.distance import VincentyDistance


def latLonToXY(lat, lon):
	pt = geopy.Point(lat, lon)
	ptRadar = geopy.Point(common.KTLXLocation[0], common.KTLXLocation[1])
	dest = geopy.distance.distance(pt, ptRadar).nm

	lat1 = lat*(math.pi/180.0)
	lat2 = common.KTLXLocation[0]*(math.pi/180.0)
	dLat = (common.KTLXLocation[0] - lat)*(math.pi/180.0)
	dLon = (common.KTLXLocation[1] - lon)*(math.pi/180.0)

	# calculate angle
	angle = math.atan2(math.sin(dLon)*math.cos(lat2), math.cos(lat1)*math.sin(lat2) - \
														math.sin(lat1)*math.cos(lat2)*math.cos(dLon))*(180/math.pi)

	return ((180+(angle+360))%360, distToY(dest))

def xyToLatLon(x, y):
	ptRadar = geopy.Point(common.KTLXLocation[0], common.KTLXLocation[1])
	pt2 = VincentyDistance(kilometers = (yToDist(y))/0.6214*1.15).destination(ptRadar, x)
	return (pt2.latitude, pt2.longitude)

def latLonDist(pt1, pt2):
	geoPt1 = geopy.Point(pt1[0], pt1[1])
	geoPt2 = geopy.Point(pt2[0], pt2[1])
	
	# nautical miles
	return geopy.distance.distance(geoPt1, geoPt2).nm

def latLonFlatHeading(pt1, pt2):

	xChg = pt1[1]-pt2[1]
	yChg = pt2[0]-pt1[0]

	return (math.degrees(math.atan2(yChg,xChg))+270.0)%360.0

def hdg(xChg, yChg):
	return (math.degrees(math.atan2(yChg,xChg))+270.0)%360.0

def hdgSlope(slope, xChg, yChg):
	corr = 0
	if yChg < 0:
		corr = 180

	return (math.degrees(math.atan(slope))+corr)%360.0


def hdgSlopeTest():
	p1 = (35.00, -97.00)
	p2 = (36.00, -96.00)# NE
	p3 = (36.00, -98.00)# NW
	p4 = (34.00, -96.00)# SE
	p5 = (34.00, -98.00)# SW
	
	h1 = hdgSlope((p2[0]-p1[0])/(p2[1]-p1[1]), (p2[1]-p1[1]), (p2[0]-p1[0]))
	print 'h1 = %.2f'%h1
	h2 = hdgSlope((p3[0]-p1[0])/(p3[1]-p1[1]), (p3[1]-p1[1]), (p3[0]-p1[0]))
	print 'h2 = %.2f'%h2
	h3 = hdgSlope((p4[0]-p1[0])/(p4[1]-p1[1]), (p4[1]-p1[1]), (p4[0]-p1[0]))
	print 'h3 = %.2f'%h3
	h4 = hdgSlope((p5[0]-p1[0])/(p5[1]-p1[1]), (p5[1]-p1[1]), (p5[0]-p1[0]))
	print 'h4 = %.2f'%h4

def hdgDiff(hdg1, hdg2):
	if hdg1 == 360: hdg1 = 0.0
	if hdg2 == 360: hdg2 = 0.0
	cw = hdg2 - hdg1
	ccw = 360 - (hdg2 + hdg1)
	if abs(cw) <= abs(ccw):
		return float(cw)
	else:
		return float(-ccw)

def distToY(dist):
	return  common.RadarRange-(dist/common.RadarDistIncrement)
	
def yToDist(y):
	return (common.RadarRange-y)*common.RadarDistIncrement

def radToDeg(r):
	return r*(180.0/math.pi)

def degToRad(d):
	return d*(math.pi/180.0)

def ptLineDist(lineStart, lineEnd, pt):
	""" This assumes the three points are x/y coordinate tuples """

	px = lineEnd[0]-lineStart[0]
	py = lineEnd[1]-lineStart[1]

	something = px*px + py*py

	u =  ((pt[0] - lineStart[0]) * px + (pt[1] - lineStart[1]) * py) / float(something)

	if u > 1:
		u = 1
	elif u < 0:
		u = 0

	x = lineStart[0] + u * px
	y = lineStart[1] + u * py

	dx = x - pt[0]
	dy = y - pt[1]

	dist = math.sqrt(dx*dx + dy*dy)

	return dist
